home *** CD-ROM | disk | FTP | other *** search
/ Amiga Tools 3 / Amiga Tools 3.iso / grafik / raytracing / rayshade-4.0.6.3 / inetray / inetray.start.c < prev    next >
C/C++ Source or Header  |  1993-08-17  |  5KB  |  185 lines

  1. /*======================================================================
  2.             I N E T R A Y . S T A R T . C 
  3.                     doc: Fri Mar 13 12:21:13 1992
  4.                     dlm: Tue Aug 17 09:52:44 1993
  5.                     (c) 1992 ant@julia
  6.                     uE-Info: 27 6 T 0 0 72 2 2 8 ofnI
  7. ======================================================================*/
  8.  
  9. /* Note: Up to version 1.1.0 more than 1 servers could be started by
  10.          this program. As of 1.1.1 handling of more than one sever has
  11.          been moved to rpc.inetrayd for consistency. Variables have,
  12.          however, not been renamed and the structure still reflects the
  13.          olde approach. */
  14.  
  15. #define        DAEMON            /* run as daemon */
  16.  
  17. #include    <stdio.h>
  18. #include    <signal.h>
  19. #include    <syslog.h>
  20. #include    <sys/types.h>
  21. #include    <sys/ioctl.h>
  22. #include    <sys/stat.h>
  23. #include    <sys/param.h>
  24. #include    <rpc/rpc.h>
  25. #ifndef TIOCNOTTY
  26. #include    <termios.h>
  27. #endif
  28. #include    "inetray.h"
  29. #include    "config.h"
  30. #include    "common.h"
  31. #include    "stderr.h"
  32. #include    "inetray.start.h"
  33.  
  34. static int     nRunning = 0;        /* number of running servers */
  35. static char     *av0 = NULL;        /* server path */
  36.  
  37. extern void starter_1();        /* generated dispatch routine */
  38.  
  39. static void cleanUp(sig)        /* clean up on kill */
  40. int sig;
  41. {
  42.     if (sig >= 0) 
  43.         SYSLOG(LOG_ERR,"Shut down due to signal #%d",sig);
  44.     (void) pmap_unset(STARTER, IRSV1);
  45.     CLOSELOG();
  46.     if (sig >= 0) exit(sig);
  47.     else          exit(1);
  48. }
  49.  
  50. main(ac,av)
  51. int ac; char *av[];
  52. {
  53.     int     i,pid,sock,proto,rpcStat;
  54.     char    name[MAXHOSTNAMELEN];
  55.     register SVCXPRT *transp;
  56.     struct stat buf;
  57.     
  58.     OPENLOG(av[0]);            /* set up syslog */
  59.  
  60.     for (i=0; i<NSIG; i++)        /* clean termination */
  61.         signal(i,cleanUp);
  62.         
  63.     if (gethostname(name,MAXHOSTNAMELEN) < 0) {
  64.         perror("gethostname");
  65.         cleanUp(-1);
  66.     }
  67.     rpcStat = callrpc(name,STARTER,IRSV1,0,xdr_void,NULL,xdr_void,NULL);
  68.     if (rpcStat == RPC_SUCCESS) {
  69.         fprintf(stderr,"%s already running on %s\n",av[0],name);
  70.         CLOSELOG();
  71.         exit(1);
  72.     } else if (rpcStat != RPC_PROGNOTREGISTERED &&
  73.            rpcStat != RPC_TIMEDOUT) {
  74.         fprintf(stderr,"callrpc(%s,0) ",name);
  75.         clnt_perrno(rpcStat);
  76.         fprintf(stderr,"\n");
  77.         cleanUp(-1);
  78.     }
  79.  
  80.     if (ac != 2) {
  81.         fprintf(stderr,"Usage: %s <executable>\n",av[0]);
  82.         cleanUp(-1);
  83.     }
  84.     av0 = av[1];            /* set server path */
  85.     if (stat(av0,&buf) < 0) {
  86.         perror("stat: cannot find executable");
  87.         cleanUp(-1);
  88.     }
  89.  
  90.     signal(SIGIO,SIG_IGN);        /* rpc.inetrayd uses this */
  91.     signal(SIGPIPE,SIG_IGN);    /* rpc.inetrayd uses this */
  92.     
  93. #ifdef DAEMON
  94.     pid = fork();
  95.         if (pid < 0) {
  96.                 perror("cannot fork");
  97.                 cleanUp(-1);
  98.         }
  99.         if (pid) exit(0);
  100.         for (i = 0 ; i < 20; i++)
  101.                 (void) close(i);
  102.         i = open("/dev/console", 2);
  103.         (void) dup2(i, 1);
  104.         (void) dup2(i, 2);
  105. #ifndef KEEPTTY_QUIRK
  106.         i = open("/dev/tty", 2);
  107.         if (i >= 0) {
  108.                 (void) ioctl(i, TIOCNOTTY, (char *)NULL);
  109.                 (void) close(i);
  110.         }
  111. #endif
  112. #endif
  113.     sock = RPC_ANYSOCK;
  114.     proto = IPPROTO_UDP;
  115.     (void) pmap_unset(STARTER, IRSV1);
  116.  
  117.     transp = svcudp_create(sock);
  118.     if (transp == NULL) {
  119.         SYSLOG(LOG_ERR,"cannot create udp service.");
  120.         cleanUp(-1);
  121.     }
  122.     if (!svc_register(transp, STARTER, IRSV1, starter_1, proto)) {
  123.         SYSLOG(LOG_ERR,"unable to register (STARTER, IRSV1, udp).");
  124.         cleanUp(-1);
  125.     }
  126.     svc_run();
  127.     SYSLOG(LOG_ERR,"svc_run returned");
  128.     cleanUp(-1);
  129.     /* NOTREACHED */
  130. }
  131.  
  132. static void funeral()                /* bury dead children */
  133. {    
  134.     int    status;
  135.     
  136. #ifndef DAEMON
  137.     printf("first child died!\n");
  138. #endif
  139.     signal(SIGCHLD,SIG_DFL);        /* wait for all now */
  140.     while (nRunning > 0) {
  141.         wait(&status);            /* moment of rememberance */
  142. #ifndef DAEMON
  143.         printf("child returnded status %d\n",status);
  144. #endif
  145.         if (status != 0) 
  146.             SYSLOG(LOG_ERR,"child returned status %d",status);
  147.         nRunning--;            /* accounting */
  148.     }
  149. #ifndef DAEMON
  150.     printf("all children died!\n");
  151. #endif
  152. }
  153.  
  154. void *start_1()                    /* start # of servers */
  155. {
  156.     int    i,pid;
  157.     static char result;
  158.  
  159. #ifndef DAEMON
  160.     printf("START request received...\n");
  161. #endif
  162.     if (nRunning != 0)            /* already servicing */
  163.         return;
  164.     signal(SIGCHLD,funeral);        /* wait for children */
  165.     pid = fork();
  166.     if (pid < 0) {
  167.         SYSLOG(LOG_ERR,"fork: %m");
  168.         cleanUp(-1);
  169.     }
  170.     if (pid == 0) {                /* worker */
  171. #ifndef DAEMON
  172.         printf("execl: %s rpc.inetrayd STARTER\n",av0);
  173. #endif
  174.         if (execl(av0,"rpc.inetrayd","STARTER",NULL) < 0) {
  175.             SYSLOG(LOG_ERR,"execl: %m");
  176.             cleanUp(-1);
  177.         }
  178.     }
  179. #ifndef DAEMON
  180.     printf("pid %d forked...\n",pid);
  181. #endif
  182.     nRunning++;                /* one started */
  183. }
  184.  
  185.